from websocket_server import WebsocketServer
from PIL import ImageGrab
import time
import pyautogui
import autopy
import random
flag=True;
def new_client(client, server):
	i = 0;
	print("New client connected and was given id %d" % client['id'])
		#im = ImageGrab.grab()  # 可以添加一个坐标元组进去
		#im.save('E:\\des\\\\'+str(i)+'.jpg')
	img = pyautogui.screenshot()  # x,y,w,h
	img.save('E:\\des\\\\'+str(i)+'.jpg')
		# 图片转换为字节
	server.send_message_to_all(str(i));

# Called for every client disconnecting
def client_left(client, server):
	print("Client(%d) disconnected" % client['id'])


# Called when a client sends a message
def message_received(client, server, message):
	print(message);
	array = message.split(',');
	autopy.mouse.move(int(array[0]), int(array[1]))  # 平滑移动鼠标（上面那个是瞬间的）
	autopy.mouse.click(autopy.mouse.click(autopy.mouse.Button.LEFT))  # 单击
	img = pyautogui.screenshot()  # x,y,w,h
	img.save('E:\\des\\\\' + '0' + '.jpg')
	server.send_message_to_all(str(random.random()))


#	global i;
#	i=i+1;
#	img = pyautogui.screenshot()  # x,y,w,h
#	img.save('E:\\des\\\\' + str(i) + '.jpg')
#	server.send_message_to_all(str(i));

	# 图片转换为字节


PORT=9001
server = WebsocketServer(PORT,'192.168.1.5')
server.set_fn_new_client(new_client)
server.set_fn_client_left(client_left)
server.set_fn_message_received(message_received)
server.run_forever()
